package cn.edu.neusoft.food729.model;

import java.util.List;

import cn.edu.neusoft.food729.Bean.UserCollectionBean;
import cn.edu.neusoft.food729.common.Common;
import cn.edu.neusoft.food729.iface.UserCollectioniface;
import cn.edu.neusoft.food729.listener.UserCollectionListener;
import cn.edu.neusoft.food729.service.UserCollectionService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserCollectionModel implements UserCollectioniface {
    private Retrofit retrofit;

    public UserCollectionModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getUserCollectionResult(int user_id, int flag, final UserCollectionListener userCollectionListener) {
        UserCollectionService service = retrofit.create(UserCollectionService.class);
        Call<List<UserCollectionBean>> call = service.getAllUserCollection(user_id, flag);

        call.enqueue(new Callback<List<UserCollectionBean>>() {
            @Override
            public void onResponse(Call<List<UserCollectionBean>> call, Response<List<UserCollectionBean>> response) {
                if (response.isSuccessful() && response.body() != null)
                    userCollectionListener.onResponse(response.body());
                else userCollectionListener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<List<UserCollectionBean>> call, Throwable t) {
                userCollectionListener.onFail(t.toString());
                System.out.println("--DemoModel--" + t.toString());

            }
        });
    }
}

